﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class TStage : System.Web.UI.Page
    {
        private ProjectMainManager objProjectMainManager = new ProjectMainManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        int workParentId;
        int workMouldId;
        public int pjCount;
        public int statusNo;
        public int fromPage;
        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            fromPage = Convert.ToInt32(Request.QueryString["FromPage"]);
            if (!IsPostBack)
            {
                try
                {
                    ProjectMain PJMian = objProjectMainManager.GetDateTemp(workParentId);
                    workMouldId = objWorkflowManager.GetWorkflowMouldId(workParentId);
                    if (PJMian != null)
                    {
                        txtPJNo.Text = PJMian.PJId;
                        ddlStage.SelectedValue = PJMian.StageName;
                        string date = PJMian.RealEndDate.ToString("yyyy-MM-dd");
                        string str = string.Format("<script>document.getElementById('{0}').value = '{1}'</script>;", "txtEndDate", date);
                        txtData.Text = PJMian.Statues == "1" ? "已同步" : "未同步";
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                    }
                    pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, workMouldId);
                    statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                    if(fromPage==0 || statusNo!=100)
                    {
                        if (pjCount > 0 && statusNo < 1)
                        {
                            if (PJMian != null) { BtnUpdate.Visible = true; }
                            else { BtnCreate.Visible = true; }
                            txtPJNo.Enabled = true;
                            ddlStage.Enabled = true;
                        }
                        else if ((pjCount > 0 && statusNo == 2))
                        {
                            BtnSubmit.Visible = true;

                        }
                    }
                    if (statusNo == 100)
                    {
                        BtnSubmit.Visible = true;
                        BtnCreate.Visible = false;
                        BtnUpdate.Visible = false;
                    }



                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
        }

        protected void BtnSubmit1_Click(object sender, EventArgs e)
        {
            try
            {
                ProjectMain PJMain = new ProjectMain()
                {
                    WorkParentId = workParentId,
                    PJId = txtPJNo.Text.Trim().ToUpper(),
                    StageName = ddlStage.SelectedValue,
                    RealEndDate = Request.Params.Get("txtEndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtEndDate"))
                };
                int count = objProjectMainManager.GetCount(PJMain.PJId, PJMain.StageName, null);
                if (count >= 1)
                {
                    string msg = string.Format("<script>alert('已申请过转阶段')</script>");
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                    return;
                }
                if (PJMain.StageName != "T1")
                {
                    int tNum = int.Parse(PJMain.StageName.Substring(1));
                    count = objProjectMainManager.GetCount(PJMain.PJId, "T" + (tNum-1), "1");
                    if (count == 0)
                    {
                        string msg = string.Format("<script>alert('请先完成之前的转阶段')</script>");
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                        return;
                    }
                }
                count = objProjectMainManager.GetCount(PJMain.PJId);
                if (count == 0)
                {
                    string msg = string.Format("<script>alert('没有找到{0}')</script>", PJMain.PJId);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                    return;
                }
                objProjectMainManager.InsertDateTemp(PJMain);
                string msg1 = string.Format("<script>alert('成功！')</script>");
                string str1 = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg1);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str1);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void BtnSubmit2_Click(object sender, EventArgs e)
        {
            ProjectMain PJMain = new ProjectMain()
            {
                WorkParentId = workParentId,
                PJId = txtPJNo.Text.Trim().ToUpper(),
                StageName = ddlStage.SelectedValue
            };
            try
            {
                objProjectMainManager.UpdateStatues(PJMain.PJId, PJMain.StageName);
                PJMain = objProjectMainManager.GetDateTemp(workParentId);
                objProjectMainManager.UpdateStage(PJMain.PJId, PJMain.RealEndDate,PJMain.StageName);
                string msg = string.Format("<script>alert('同步成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);

            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            ProjectMain PJMain = new ProjectMain()
            {
                WorkParentId = workParentId,
                PJId = txtPJNo.Text.Trim().ToUpper(),
                StageName = ddlStage.SelectedValue,
                RealEndDate = Request.Params.Get("txtEndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtEndDate"))
            };
            try
            {
                objProjectMainManager.UpdateStage(PJMain);
                string msg = string.Format("<script>alert('更新成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }
}